import sys
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QPushButton, \
    QFrame, QLabel
from PyQt5 import QtCore, QtGui, QtWidgets
from UI.HomeForm import Ui_Form_home
from service.home_service import HomeService,GetQtyWorker

class HomeController(QWidget,Ui_Form_home):
    def __init__(self,parent=None):
        super(HomeController,self).__init__(parent)
        self.setupUi(self)
        self.set_elements()
        self.star_worker()

    def label_qty_append(self,msg):
        self.label_robot_qty.setText(msg)
        QtWidgets.QApplication.processEvents()


    def set_elements(self):
        work_robot_qty,resume_qty, msg_qty = HomeService.get_qty()
        self.label_robot_qty.setText(str(work_robot_qty))
        self.label_resume_qty.setText(str(resume_qty))
        self.label_message_qty.setText(str(msg_qty))
    def star_worker(self):
        try:
            self.get_qty_worker = GetQtyWorker()
            self.thread =QThread()
            # 将 Worker 移动到 QThread 中
            self.get_qty_worker.moveToThread(self.thread)

            self.get_qty_worker.update_signal.connect(self.refresh_label_text)
            # 启动线程
            self.thread.started.connect(self.get_qty_worker.start_work)
            self.thread.start()
        except Exception as e:
            print(e)

    def refresh_label_text(self,work_robot_qty,resume_qty,current_time):
        self.label_robot_qty.setText(str(work_robot_qty))
        self.label_resume_qty.setText(str(resume_qty))
        self.label_message_qty.setText(str(current_time))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    home = HomeController()
    home.show()
    sys.exit(app.exec_())